import scrapy
from .. items import MaoyanItem
from lxml import etree
class MaoSpider(scrapy.Spider):
    name = "mao"
    allowed_domains = ["maoyan.com"]
    start_urls = ["https://www.maoyan.com/films?showType=1&offset=0"]

    # def start_requests(self):
    #     for offset in range(0,61,30):
    #         url=f"https://www.maoyan.com/films?showType=1&offset={offset}"
    #         yield scrapy.Request(url=url,callback=self.parse_html)
    offset=0
    def parse(self, response):
        item = MaoyanItem()
        content=response.text
        html=etree.HTML(content)
        # 元素列表
        dd_list=html.xpath('//div[@class="movie-hover-info"]')
        for dd in dd_list:
            item['_name']=''.join(dd.xpath("./div/span/text()")[0]).strip()
            item['_actors']=''.join(dd.xpath("./div")[2].xpath("./text()")).strip()
            if item['_actors']=='':
                item['_actors']='暂无可提供的信息'
            item['_time']=''.join(dd.xpath("./div")[3].xpath("./text()")).strip()

            yield item

        if self.offset<=30:
            self.offset+=30
            url='https://www.maoyan.com/films?showType=1&offset={}'.format(self.offset)
            yield scrapy.Request(url=url, callback=self.parse)